﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Xml;

using FCNS.KeepMoney.Util;

namespace Translation
{
	public static class Program
	{
		[STAThread]
		public static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

            if ((args != null) && (args.Length == 2))
            {
                try { ExecuteCmd(args[0], args[1]); }
                catch (Exception exCmd)
                {
                    MessageBox.Show(exCmd.Message, "Translation.exe",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                return;
            }

			Application.Run(new MainForm());
		}
        /// <summary>
        /// 执行语言文件编辑器程序参数
        /// </summary>
        /// <param name="strCmd">当<code>strCmd == "convert_resx"</code>创建语言文件<br/>
        /// 当<code>strCmd == "src_from_xml"</code>创建强类型资源类</param>
        /// <param name="strFile">语言文件路径</param>
        private static void ExecuteCmd(string strCmd, string strFile)
        {
            XmlDocument xmlIn = new XmlDocument();
            StreamWriter swOut;

            switch (strCmd)
            {
                case "convert_resx":
                    #region
                   swOut = new StreamWriter(strFile + ".lng.xml",
                    false, new UTF8Encoding(false));

                    xmlIn.Load(strFile);

                    foreach (XmlNode xmlChild in xmlIn.DocumentElement.ChildNodes)
                    {
                        if (xmlChild.Name != "data") continue;

                        swOut.Write("<Data Name=\"" + xmlChild.Attributes["name"].Value +
                            "\">\r\n\t<Value>" + xmlChild.SelectSingleNode("value").InnerXml +
                            "</Value>\r\n</Data>\r\n");
                    }

                    swOut.Close();
                    #endregion
                    break;
                //生成Safety使用的两个类文件，仅需要提示字段的的StringTable
                case "src_from_xml":
                    #region
                    xmlIn.Load(strFile);
                    foreach (XmlNode xmlTable in xmlIn.DocumentElement.SelectNodes("StringTable"))
                    {
                       swOut = new StreamWriter(xmlTable.Attributes["Name"].Value +
                            ".Generated.cs", false, new UTF8Encoding(false));

                        swOut.WriteLine("// 此文件由语言文件编辑器自动生成!");
                        swOut.WriteLine("// 请不要手动更改此文件，操作将被忽略。");
                        swOut.WriteLine();
                        swOut.WriteLine("using System;");
                        swOut.WriteLine("using System.Collections.Generic;");
                        swOut.WriteLine();
                        swOut.WriteLine("namespace " + xmlTable.Attributes["Namespace"].Value);
                        swOut.WriteLine("{");
                        swOut.WriteLine("\t/// <summary>");
                        swOut.WriteLine("\t/// 强类型资源类，用于查找本地化文本等等。");
                        swOut.WriteLine("\t/// </summary>");
                        swOut.WriteLine("\tpublic static class " + xmlTable.Attributes["Name"].Value);
                        swOut.WriteLine("\t{");

                        swOut.WriteLine("\t/// <summary>");
                        swOut.WriteLine("\t/// 从集合中获取与指定的键相关联的值。");
                        swOut.WriteLine("\t/// </summary>");
                        swOut.WriteLine("\t/// <param name=\"dictNew\">键和值的集合</param>");
                        swOut.WriteLine("\t/// <param name=\"strName\">键</param>");
                        swOut.WriteLine("\t/// <param name=\"strDefault\">值</param>");
                        swOut.WriteLine("\t/// <returns>值</returns>");
                        swOut.WriteLine("\t\tprivate static string TryGetEx(Dictionary<string, string> dictNew,");
                        swOut.WriteLine("\t\t\tstring strName, string strDefault)");
                        swOut.WriteLine("\t\t{");
                        swOut.WriteLine("\t\t\tstring strTemp;");
                        swOut.WriteLine();
                        swOut.WriteLine("\t\t\tif(dictNew.TryGetValue(strName, out strTemp))");
                        swOut.WriteLine("\t\t\t\treturn strTemp;");
                        swOut.WriteLine();
                        swOut.WriteLine("\t\t\treturn strDefault;");
                        swOut.WriteLine("\t\t}");
                        swOut.WriteLine();

                        swOut.WriteLine("\t/// <summary>");
                        swOut.WriteLine("\t/// 设置要翻译的字段的文本");
                        swOut.WriteLine("\t/// </summary>");
                        swOut.WriteLine("\t/// <param name=\"dictNew\">键和值的集合</param>");
                        swOut.WriteLine("\t\tpublic static void SetTranslatedStrings(Dictionary<string, string> dictNew)");
                        swOut.WriteLine("\t\t{");
                        swOut.WriteLine("\t\t\tif(dictNew == null) throw new ArgumentNullException(\"dictNew\");");
                        swOut.WriteLine();

                        foreach (XmlNode xmlData in xmlTable.SelectNodes("Data"))
                        {
                            string strName = xmlData.Attributes["Name"].Value;

                            swOut.WriteLine("\t\t\tm_str" + strName +
                                " = TryGetEx(dictNew, \"" + strName +
                                "\", m_str" + strName + ");");
                        }

                        swOut.WriteLine("\t\t}");
                        swOut.WriteLine();

                        swOut.WriteLine("\t\tprivate static readonly string[] m_vKeyNames = {");
                        XmlNodeList xNodes = xmlTable.SelectNodes("Data");
                        for (int i = 0; i < xNodes.Count; ++i)
                        {
                            XmlNode xmlData = xNodes.Item(i);
                            swOut.WriteLine("\t\t\t\"" + xmlData.Attributes["Name"].Value +
                                "\"" + ((i != xNodes.Count - 1) ? "," : string.Empty));
                        }

                        swOut.WriteLine("\t\t};");
                        swOut.WriteLine();

                        swOut.WriteLine("\t\tpublic static string[] GetKeyNames()");
                        swOut.WriteLine("\t\t{");
                        swOut.WriteLine("\t\t\treturn m_vKeyNames;");
                        swOut.WriteLine("\t\t}");

                        foreach (XmlNode xmlData in xmlTable.SelectNodes("Data"))
                        {
                            string strName = xmlData.Attributes["Name"].Value;
                            string strValue = xmlData.SelectSingleNode("Value").InnerText;
                            if (strValue.Contains("\""))
                            {
                                strValue = strValue.Replace("\"", "\"\"");
                            }

                            swOut.WriteLine();
                            swOut.WriteLine("\t\tprivate static string m_str" +
                                strName + " =");
                            swOut.WriteLine("\t\t\t@\"" + strValue + "\";");

                            swOut.WriteLine("\t\t/// <summary>");
                           // swOut.WriteLine("\t\t/// 查找  " + StrUtil.StringToHtml(strValue) + " 的本地化文本");
                            swOut.WriteLine("\t\t/// </summary>");
                            swOut.WriteLine("\t\tpublic static string " +
                                strName);
                            swOut.WriteLine("\t\t{");
                            swOut.WriteLine("\t\t\tget { return m_str" + strName +
                                "; }");
                            swOut.WriteLine("\t\t}");
                        }

                        swOut.WriteLine("\t}"); // Close class
                        swOut.WriteLine("}");

                        swOut.Close();
                    }
                    #endregion
                    break;
            }
        }
	}
}
